#!/bin/bash

if [ "$#" -lt 3 ]; then
    echo "Usage: $0 vendor product_name product_version"
    exit 1
fi

VENDOR="$1"
PRODUCT_NAME="$2"
PRODUCT_VERSION="$3"

echo "${VENDOR}/${PRODUCT_NAME}/${PRODUCT_VERSION}"

TIMESTAMP=$(date)
DATEINFO=$(date +%Y%m%d%H%M%S)

if [ -d oem ]; then
	sudo umount oem
	sudo rm -fr oem
fi
if [ -d rootfs ]; then
	sudo umount rootfs
	sudo rm -fr rootfs
fi
if [ -d dist ]; then
	sudo rm -fr dist
fi
mkdir oem
mkdir rootfs
mkdir dist

# cp -arf parts/rockdev/* rockdev/

sudo mount rockdev/oem.img oem
sudo cp -arf parts/oem/* oem/

sudo mount rockdev/rootfs.img rootfs
sudo cp -arf parts/rootdir/* rootfs


sync

if [ -f "$PRODUCT_NAME/update.sh" ]; then
$PRODUCT_NAME/update.sh $PRODUCT_NAME
fi
sync

rm *.img
IMG_FILENAME=update_pcba_${VENDOR}_${PRODUCT_NAME}_${PRODUCT_VERSION}_$(date +%Y%m%d)_$(date +%H%M%S)
echo ${IMG_FILENAME}
cd Linux_Pack_Firmware
./rk3562-mkupdate.sh

mv update.img ../dist/$IMG_FILENAME.img
cd ../

cd dist
md5sum $IMG_FILENAME.img > $IMG_FILENAME.img.md5

zip ${IMG_FILENAME}.zip ${IMG_FILENAME}.img ${IMG_FILENAME}.img.md5
rm ${IMG_FILENAME}.img ${IMG_FILENAME}.img.md5 -rf
cd ../

sudo umount rootfs
sudo umount oem
